package com.news.web.servlet.admin;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.News;
import com.news.service.NewsService;
import com.news.service.impl.NewsServiceImpl;

/**
 * Servlet implementation class AdminNewsQueryServlet
 */
@WebServlet("/AdminNewsQueryServlet")
public class AdminNewsQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String newsIdStr = request.getParameter("newsId");
		String newsType = request.getParameter("newsType");
		String newsHeadline = request.getParameter("newsHeadline");
		String newsIntro = request.getParameter("newsIntro");
		String newsAuthor = request.getParameter("newsAuthor");
		String newsDateStr = request.getParameter("newsDate");
		String newsImg = request.getParameter("newsImg");
		String newsHeatStr = request.getParameter("newsHeat");
		String newsReviewNumStr = request.getParameter("newsReviewNum");
		
		System.out.println(newsHeadline);
		
		// 定义日期格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 进行数据格式转换
		int newsId = 0;
		int newsHeat = 0;
		Timestamp newsDate = null;
		int newsReviewNum = 0;
		try {
			if (newsIdStr != null && !newsIdStr.isEmpty()) {
				newsId = Integer.parseInt(newsIdStr);
			}
			if (newsHeatStr != null && !newsHeatStr.isEmpty()) {
				newsHeat = Integer.parseInt(newsHeatStr);
			}
			if (newsDateStr != null && !newsDateStr.isEmpty()) {
				// 将字符串解析为日期
				java.util.Date parsedDate = dateFormat.parse(newsDateStr);
				newsDate = new Timestamp(parsedDate.getTime());
			}
			if (newsReviewNumStr != null && !newsReviewNumStr.isEmpty()) {
				newsReviewNum = Integer.parseInt(newsReviewNumStr);
			}
		} catch (NumberFormatException | ParseException e) {
			// 处理数值转换异常的情况
			e.printStackTrace(); // 记录日志或者向用户显示错误信息
			request.setAttribute("newsQueryLikeList", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminNewsQueryAndDelete.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		// 实例化News
		News news = new News(newsId, newsType, newsHeadline, newsIntro, newsAuthor, newsDate, newsImg,
				newsHeat, newsReviewNum);
		// 创建NewsService实例
		NewsService newsService = new NewsServiceImpl();
		// 使用模糊查询
		List<News> newsQueryLikeList = newsService.likeSelectListReview(news);
		// 将数据存入
		request.setAttribute("newsQueryLikeList", newsQueryLikeList);
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminNewsQueryAndDelete.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
